import { Injectable } from '@angular/core';
// rxjs
import { Subject } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class MissionService {
  /**
   *  通信协议 
   *  from:  组件名称  (消息发送者)  
   *  to:    组件名称  (消息接收者)
   * 
   *  missionAnnounced$ 下发的数据流
   *  missionConfirmed$ 上传的数据流
   * */

  // Observable
  private missionAnnouncedSource = new Subject<Object>();
  private missionConfirmedSource = new Subject<Object>();

  constructor() { }

 
  // Observable 数据流  
  missionAnnounced$ = this.missionAnnouncedSource.asObservable();
  missionConfirmed$ = this.missionConfirmedSource.asObservable();
 
  // Service message
  announceMission(from?: string, to?: string, mission?: any) {
    let missionInfo: Object =  new Object();
    missionInfo['from'] = from;
    missionInfo['data'] = mission;
    missionInfo['to'] = to;
    this.missionAnnouncedSource.next(missionInfo);
  }
 
  confirmMission(from?: string, to?: string, astronaut?: any) {
    let astronautInfo: Object =  new Object();
    astronautInfo['from'] = from;
    astronautInfo['data'] = astronaut;
    astronautInfo['to'] = to;
    this.missionConfirmedSource.next(astronautInfo);
    this.missionConfirmedSource.next(from);
  }
}
